from django.shortcuts import render, redirect
from app01 import models
from django import forms
from app01.utils.pagination import Pagination
from app01.utils.bootstrap import BootStrapModelForm
from app01.utils.form import AdminModelForm, AdminEditModelForm,AdminResetPasswordModelForm


def admin_list(request):
    """管理员列表"""
    #检测用户是否已经登录，已登录，继续向下走，未登录，跳转回登录页面
    #用户发来请求，获取cookie随机字符串，拿着随机字符串看看session中有没有。
    info = request.session.get("info")
    #print(info)
    if not info:
        return redirect('/login/')

    # 搜索
    data_dict = {}
    search_data = request.GET.get("q", "")
    if search_data:
        data_dict["username__contains"] = search_data
    # 根据搜索条件进行查找
    queryset = models.Admin.objects.filter(**data_dict)
    # 分页
    page_object = Pagination(request, queryset)
    contex = {
        "queryset": page_object.page_queryset,  # 分完页的数据
        "page_string": page_object.html(),  # 生成的页码
        "search_data": search_data
    }
    return render(request, 'admin_list.html', contex)


def admin_add(request):
    """添加管理员"""
    title = "新建管理员"
    if request.method == "GET":
        form = AdminModelForm()
        return render(request, 'change.html', {"form": form, "title": title})
    form = AdminModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect("/admin/list/")
    return render(request, 'change.html', {"form": form, "title": title})


def admin_edit(request, nid):
    """编辑管理员"""
    # 对象/none
    row_object = models.Admin.objects.filter(id=nid).first()
    if not row_object:
        # return redirect("/admin/list/")
        return render(request, 'error.html', {"msg": "数据不存在！"})
    title = "编辑管理员"
    if request.method == "GET":
        form = AdminEditModelForm(instance=row_object)
        return render(request, "change.html", {"form": form, "title": title})
    form = AdminEditModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return redirect("/admin/list/")
    return render(request, "change.html", {"form": form, "title": title})

# def admin_delete(request):
#     """删除管理员"""
#     nid = request.GET.get("nid")
#     models.Admin.objects.filter(id = nid).delete()
#     return redirect("/admin/list/")

def admin_delete(request,nid):
    """删除管理员"""
    models.Admin.objects.filter(id = nid).delete()
    return redirect("/admin/list/")

def admin_reset(request,nid):
    """重置管理员密码"""
    # 对象/none
    row_object = models.Admin.objects.filter(id=nid).first()
    if not row_object:
        return redirect("/admin/list/")
    title = "重置密码 - {}".format(row_object.username)
    if request.method == "GET":
        form = AdminResetPasswordModelForm()
        return render(request, "change.html", {"form":form,"title": title})
    #表单验证
    form = AdminResetPasswordModelForm(data=request.POST,instance=row_object)
    if form.is_valid():
        form.save()
        return redirect("/admin/list/")
    return render(request, "change.html", {"form": form, "title": title})